
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>4.5. (@)observer · MobX 中文文档</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.2">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-anchors/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../best/react.html" />
    
    
    <link rel="prev" href="reaction.html" />
    

    <style>
    @media only screen and (max-width: 640px) {
        .book-header .hidden-mobile {
            display: none;
        }
    }
    </style>
    <script>
        window["gitbook-plugin-github-buttons"] = {"buttons":[{"user":"SangKa","repo":"MobX-Docs-CN","type":"star","size":"small","count":true},{"user":"SangKa","repo":"MobX-Docs-CN","type":"fork","size":"small","count":true}]};
    </script>

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    1. MobX 介绍
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.1" data-path="../intro/overview.html">
            
                <a href="../intro/overview.html">
            
                    
                    1.1. MobX 要点
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.2" data-path="../intro/concepts.html">
            
                <a href="../intro/concepts.html">
            
                    
                    1.2. 概念与原则
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="api.html">
            
                <a href="api.html">
            
                    
                    2. API概览
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <span>
            
                    
                    3. observable 相关
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="observable.html">
            
                <a href="observable.html">
            
                    
                    3.1. observable
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="observable-decorator.html">
            
                <a href="observable-decorator.html">
            
                    
                    3.2. @observable
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="object.html">
            
                <a href="object.html">
            
                    
                    3.3. objects
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="array.html">
            
                <a href="array.html">
            
                    
                    3.4. arrays
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="map.html">
            
                <a href="map.html">
            
                    
                    3.5. maps
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="boxed.html">
            
                <a href="boxed.html">
            
                    
                    3.6. boxed values
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="modifiers.html">
            
                <a href="modifiers.html">
            
                    
                    3.7 decorators
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    4. 对 observables 作出响应
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="computed-decorator.html">
            
                <a href="computed-decorator.html">
            
                    
                    4.1. (@)computed
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="autorun.html">
            
                <a href="autorun.html">
            
                    
                    4.2. autorun
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="when.html">
            
                <a href="when.html">
            
                    
                    4.3. when
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="reaction.html">
            
                <a href="reaction.html">
            
                    
                    4.4. reaction
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.4.5" data-path="observer-component.html">
            
                <a href="observer-component.html">
            
                    
                    4.5. (@)observer
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="../best/react.html">
            
                <a href="../best/react.html">
            
                    
                    4.6. 理解 MobX 对何作出响应
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" >
            
                <span>
            
                    
                    5. 改变 observables
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="action.html">
            
                <a href="action.html">
            
                    
                    5.1. action
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../best/actions.html">
            
                <a href="../best/actions.html">
            
                    
                    5.2. async actions & flows
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="object-api.html">
            
                <a href="object-api.html">
            
                    
                    5.3. Object api
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" >
            
                <span>
            
                    
                    6. 工具函数
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="tojson.html">
            
                <a href="tojson.html">
            
                    
                    6.1. toJS
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="extend-observable.html">
            
                <a href="extend-observable.html">
            
                    
                    6.2. extendObservable
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="extending.html">
            
                <a href="extending.html">
            
                    
                    6.3. createAtom
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="observe.html">
            
                <a href="observe.html">
            
                    
                    6.4. intercept & observe
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.5" >
            
                <a target="_blank" href="https://github.com/mobxjs/mobx-utils#frompromise">
            
                    
                    6.5. mobxUtils.fromPromise
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.6" >
            
                <a target="_blank" href="https://github.com/mobxjs/mobx-utils#fromresource">
            
                    
                    6.6. mobxUtils.fromResource
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.7" >
            
                <a target="_blank" href="https://github.com/mobxjs/mobx-utils#tostream">
            
                    
                    6.7. mobxUtils.toStream
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.8" >
            
                <a target="_blank" href="https://github.com/mobxjs/mobx-utils#fromstream">
            
                    
                    6.8. mobxUtils.fromStream
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.9" >
            
                <a target="_blank" href="https://github.com/mobxjs/mobx-utils#now">
            
                    
                    6.9. mobxUtils.now
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.10" data-path="create-transformer.html">
            
                <a href="create-transformer.html">
            
                    
                    6.10. mobxUtils.createTransformer
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.11" data-path="expr.html">
            
                <a href="expr.html">
            
                    
                    6.11. mobxUtils.expr
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.12" data-path="api.html">
            
                <a href="api.html">
            
                    
                    6.12. complete api overview
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" >
            
                <a target="_blank" href="https://github.com/mobxjs/awesome-mobx#examples">
            
                    
                    7. 博客、 视频及相关项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" >
            
                <span>
            
                    
                    8. 贴士与技巧
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../faq/faq.html">
            
                <a href="../faq/faq.html">
            
                    
                    8.1. 常见问题解答
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="../best/pitfalls.html">
            
                <a href="../best/pitfalls.html">
            
                    
                    8.2. 常见陷阱与最佳实践
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.3" data-path="../best/decorators.html">
            
                <a href="../best/decorators.html">
            
                    
                    8.3. 如何(不)使用装饰器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.4" data-path="../best/trace.html">
            
                <a href="../best/trace.html">
            
                    
                    8.4. 使用 trace 进行调试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.5" data-path="../best/store.html">
            
                <a href="../best/store.html">
            
                    
                    8.5. 定义数据存储
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.6" data-path="../best/react-performance.html">
            
                <a href="../best/react-performance.html">
            
                    
                    8.6. 优化 React 组件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.7" data-path="../best/devtools.html">
            
                <a href="../best/devtools.html">
            
                    
                    8.7. 开发工具
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.8" data-path="spy.html">
            
                <a href="spy.html">
            
                    
                    8.8. spy
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../donating.html">
            
                <a href="../donating.html">
            
                    
                    9. 捐赠
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >4.5. (@)observer</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="observer"><a name="observer" class="plugin-anchor" href="#observer"><i class="fa fa-link" aria-hidden="true"></i></a>@observer</h1>
<details>
    <summary style="color: white; background:green;padding:5px;margin:5px;border-radius:2px">egghead.io &#x7B2C;1&#x8BFE;: observable &amp; observer</summary>
    <br>
    <div style="padding:5px;">
        <iframe style="border: none;" width="760" height="427" src="https://egghead.io/lessons/javascript-sync-the-ui-with-the-app-state-using-mobx-observable-and-observer-in-react/embed">
    </iframe></div>
    <a style="font-style:italic;padding:5px;margin:5px;" href="https://egghead.io/lessons/javascript-sync-the-ui-with-the-app-state-using-mobx-observable-and-observer-in-react" target="_blank">&#x5728; egghead.io &#x4E0A;&#x89C2;&#x770B;</a>
</details>

<p><code>observer</code> &#x51FD;&#x6570;/&#x88C5;&#x9970;&#x5668;&#x53EF;&#x4EE5;&#x7528;&#x6765;&#x5C06; React &#x7EC4;&#x4EF6;&#x8F6C;&#x53D8;&#x6210;&#x54CD;&#x5E94;&#x5F0F;&#x7EC4;&#x4EF6;&#x3002;
&#x5B83;&#x7528; <code>mobx.autorun</code> &#x5305;&#x88C5;&#x4E86;&#x7EC4;&#x4EF6;&#x7684; render &#x51FD;&#x6570;&#x4EE5;&#x786E;&#x4FDD;&#x4EFB;&#x4F55;&#x7EC4;&#x4EF6;&#x6E32;&#x67D3;&#x4E2D;&#x4F7F;&#x7528;&#x7684;&#x6570;&#x636E;&#x53D8;&#x5316;&#x65F6;&#x90FD;&#x53EF;&#x4EE5;&#x5F3A;&#x5236;&#x5237;&#x65B0;&#x7EC4;&#x4EF6;&#x3002;
<code>observer</code> &#x662F;&#x7531;&#x5355;&#x72EC;&#x7684; <code>mobx-react</code> &#x5305;&#x63D0;&#x4F9B;&#x7684;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> {observer} <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;mobx-react&quot;</span>;

<span class="hljs-keyword">var</span> timerData = observable({
    secondsPassed: <span class="hljs-number">0</span>
});

setInterval(() =&gt; {
    timerData.secondsPassed++;
}, <span class="hljs-number">1000</span>);

@observer <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Timer</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
    render() {
        <span class="hljs-keyword">return</span> (<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>Seconds passed: { this.props.timerData.secondsPassed } <span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span></span> )
    }
};

ReactDOM.render(<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Timer</span> <span class="hljs-attr">timerData</span>=<span class="hljs-string">{timerData}</span> /&gt;</span>, document.body);
</span></code></pre>
<p>&#x5C0F;&#x8D34;&#x58EB;: &#x5F53; <code>observer</code> &#x9700;&#x8981;&#x7EC4;&#x5408;&#x5176;&#x5B83;&#x88C5;&#x9970;&#x5668;&#x6216;&#x9AD8;&#x9636;&#x7EC4;&#x4EF6;&#x65F6;&#xFF0C;&#x8BF7;&#x786E;&#x4FDD; <code>observer</code> &#x662F;&#x6700;&#x6DF1;&#x5904;(&#x7B2C;&#x4E00;&#x4E2A;&#x5E94;&#x7528;)&#x7684;&#x88C5;&#x9970;&#x5668;&#xFF0C;&#x5426;&#x5219;&#x5B83;&#x53EF;&#x80FD;&#x4EC0;&#x4E48;&#x90FD;&#x4E0D;&#x505A;&#x3002;</p>
<p>&#x6CE8;&#x610F;&#xFF0C;&#x4F7F;&#x7528; <code>@observer</code> &#x88C5;&#x9970;&#x5668;&#x662F;&#x53EF;&#x9009;&#x7684;&#xFF0C;&#x5B83;&#x548C; <code>observer(class Timer ... { })</code> &#x8FBE;&#x5230;&#x7684;&#x6548;&#x679C;&#x662F;&#x4E00;&#x6837;&#x7684;&#x3002;</p>
<h2 id="&#x9677;&#x9631;-&#x7EC4;&#x4EF6;&#x4E2D;&#x7684;&#x95F4;&#x63A5;&#x5F15;&#x7528;&#x503C;"><a name="&#x9677;&#x9631;-&#x7EC4;&#x4EF6;&#x4E2D;&#x7684;&#x95F4;&#x63A5;&#x5F15;&#x7528;&#x503C;" class="plugin-anchor" href="#&#x9677;&#x9631;-&#x7EC4;&#x4EF6;&#x4E2D;&#x7684;&#x95F4;&#x63A5;&#x5F15;&#x7528;&#x503C;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x9677;&#x9631;: &#x7EC4;&#x4EF6;&#x4E2D;&#x7684;&#x95F4;&#x63A5;&#x5F15;&#x7528;&#x503C;</h2>
<p>MobX &#x53EF;&#x4EE5;&#x505A;&#x5F88;&#x591A;&#x4E8B;&#xFF0C;&#x4F46;&#x662F;&#x5B83;&#x65E0;&#x6CD5;&#x4F7F;&#x539F;&#x59CB;&#x6570;&#x636E;&#x7C7B;&#x578B;&#x503C;&#x8F6C;&#x53D8;&#x6210;&#x53EF;&#x89C2;&#x5BDF;&#x7684;(&#x5C3D;&#x7BA1;&#x5B83;&#x53EF;&#x4EE5;&#x7528;&#x5BF9;&#x8C61;&#x6765;&#x5305;&#x88C5;&#x5B83;&#x4EEC;&#xFF0C;&#x53C2;&#x89C1; <a href="boxed.html">boxed observables</a>)&#x3002;
&#x6240;&#x4EE5;<strong>&#x503C;</strong>&#x662F;&#x4E0D;&#x53EF;&#x89C2;&#x5BDF;&#x7684;&#xFF0C;&#x4F46;&#x662F;&#x5BF9;&#x8C61;&#x7684;<strong>&#x5C5E;&#x6027;</strong>&#x53EF;&#x4EE5;&#x3002;&#x8FD9;&#x610F;&#x5473;&#x7740; <code>@observer</code> &#x5B9E;&#x9645;&#x4E0A;&#x662F;&#x5BF9;&#x95F4;&#x63A5;&#x5F15;&#x7528;(dereference)&#x503C;&#x7684;&#x53CD;&#x5E94;&#x3002;
&#x90A3;&#x4E48;&#x5728;&#x4E0A;&#x9762;&#x7684;&#x793A;&#x4F8B;&#x4E2D;&#xFF0C;&#x5982;&#x679C;&#x662F;&#x7528;&#x4E0B;&#x9762;&#x8FD9;&#x79CD;&#x65B9;&#x5F0F;&#x521D;&#x59CB;&#x5316;&#x7684;&#xFF0C;<code>Timer</code> &#x7EC4;&#x4EF6;&#x662F;<strong>&#x4E0D;&#x4F1A;</strong>&#x6709;&#x53CD;&#x5E94;&#x7684;:</p>
<pre><code class="lang-javascript">React.render(<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Timer</span> <span class="hljs-attr">timerData</span>=<span class="hljs-string">{timerData.secondsPassed}</span> /&gt;</span>, document.body)
</span></code></pre>
<p>&#x5728;&#x8FD9;&#x4E2A;&#x4EE3;&#x7801;&#x7247;&#x6BB5;&#x4E2D;&#x53EA;&#x662F;&#x628A; <code>secondsPassed</code> &#x7684;&#x5F53;&#x524D;&#x503C;&#x4F20;&#x9012;&#x7ED9;&#x4E86; <code>Timer</code> &#x7EC4;&#x4EF6;&#xFF0C;&#x8FD9;&#x4E2A;&#x503C;&#x662F;&#x4E0D;&#x53EF;&#x53D8;&#x503C;<code>0</code>(JS&#x4E2D;&#x6240;&#x6709;&#x7684;&#x539F;&#x59CB;&#x7C7B;&#x578B;&#x503C;&#x90FD;&#x662F;&#x4E0D;&#x53EF;&#x53D8;&#x7684;)&#x3002;
&#x8FD9;&#x4E2A;&#x6570;&#x503C;&#x6C38;&#x8FDC;&#x90FD;&#x4E0D;&#x4F1A;&#x6539;&#x53D8;&#xFF0C;&#x56E0;&#x6B64; <code>Timer</code> &#x7EC4;&#x4EF6;&#x4E0D;&#x4F1A;&#x66F4;&#x65B0;&#x3002;<code>secondsPassed</code>&#x7684;&#x503C;&#x5C06;&#x6765;&#x4F1A;&#x53D1;&#x751F;&#x6539;&#x53D8;&#xFF0C;
&#x6240;&#x4EE5;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x5728;&#x7EC4;&#x4EF6;<strong>&#x4E2D;</strong>&#x8BBF;&#x95EE;&#x5B83;&#x3002;&#x6216;&#x8005;&#x6362;&#x53E5;&#x8BDD;&#x8BF4;: &#x503C;&#x9700;&#x8981;<strong>&#x901A;&#x8FC7;&#x5F15;&#x7528;</strong>&#x6765;&#x4F20;&#x9012;&#x800C;&#x4E0D;&#x662F;&#x901A;&#x8FC7;(&#x5B57;&#x9762;&#x91CF;)&#x503C;&#x6765;&#x4F20;&#x9012;&#x3002;</p>
<h2 id="es5-&#x652F;&#x6301;"><a name="es5-&#x652F;&#x6301;" class="plugin-anchor" href="#es5-&#x652F;&#x6301;"><i class="fa fa-link" aria-hidden="true"></i></a>ES5 &#x652F;&#x6301;</h2>
<p>&#x5728;ES5&#x73AF;&#x5883;&#x4E2D;&#xFF0C;&#x53EF;&#x4EE5;&#x7B80;&#x5355;&#x5730;&#x4F7F;&#x7528; <code>observer(React.createClass({ ...</code> &#x6765;&#x5B9A;&#x4E49;&#x89C2;&#x5BDF;&#x8005;&#x7EC4;&#x4EF6;&#x3002;&#x8FD8;&#x53EF;&#x4EE5;&#x53C2;&#x89C1;<a href="../best/syntax.md">&#x8BED;&#x6CD5;&#x6307;&#x5357;</a>&#x3002;</p>
<h2 id="&#x65E0;&#x72B6;&#x6001;&#x51FD;&#x6570;&#x7EC4;&#x4EF6;"><a name="&#x65E0;&#x72B6;&#x6001;&#x51FD;&#x6570;&#x7EC4;&#x4EF6;" class="plugin-anchor" href="#&#x65E0;&#x72B6;&#x6001;&#x51FD;&#x6570;&#x7EC4;&#x4EF6;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x65E0;&#x72B6;&#x6001;&#x51FD;&#x6570;&#x7EC4;&#x4EF6;</h2>
<p>&#x4E0A;&#x9762;&#x7684; <code>Timer</code> &#x7EC4;&#x4EF6;&#x8FD8;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x4F7F;&#x7528; <code>observer</code> &#x4F20;&#x9012;&#x7684;&#x65E0;&#x72B6;&#x6001;&#x51FD;&#x6570;&#x7EC4;&#x4EF6;&#x6765;&#x7F16;&#x5199;:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> {observer} <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;mobx-react&quot;</span>;

<span class="hljs-keyword">const</span> Timer = observer(({ timerData }) =&gt;
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>Seconds passed: { timerData.secondsPassed } <span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span></span>
);
</code></pre>
<h2 id="&#x53EF;&#x89C2;&#x5BDF;&#x7684;&#x5C40;&#x90E8;&#x7EC4;&#x4EF6;&#x72B6;&#x6001;"><a name="&#x53EF;&#x89C2;&#x5BDF;&#x7684;&#x5C40;&#x90E8;&#x7EC4;&#x4EF6;&#x72B6;&#x6001;" class="plugin-anchor" href="#&#x53EF;&#x89C2;&#x5BDF;&#x7684;&#x5C40;&#x90E8;&#x7EC4;&#x4EF6;&#x72B6;&#x6001;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x53EF;&#x89C2;&#x5BDF;&#x7684;&#x5C40;&#x90E8;&#x7EC4;&#x4EF6;&#x72B6;&#x6001;</h2>
<p>&#x5C31;&#x50CF;&#x666E;&#x901A;&#x7C7B;&#x4E00;&#x6837;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x4F7F;&#x7528; <code>@observable</code> &#x88C5;&#x9970;&#x5668;&#x5728;React&#x7EC4;&#x4EF6;&#x4E0A;&#x5F15;&#x5165;&#x53EF;&#x89C2;&#x5BDF;&#x5C5E;&#x6027;&#x3002;
&#x8FD9;&#x610F;&#x5473;&#x7740;&#x4F60;&#x53EF;&#x4EE5;&#x5728;&#x7EC4;&#x4EF6;&#x4E2D;&#x62E5;&#x6709;&#x529F;&#x80FD;&#x540C;&#x6837;&#x5F3A;&#x5927;&#x7684;&#x672C;&#x5730;&#x72B6;&#x6001;(local state)&#xFF0C;&#x800C;&#x4E0D;&#x9700;&#x8981;&#x901A;&#x8FC7; React &#x7684;&#x5197;&#x957F;&#x548C;&#x5F3A;&#x5236;&#x6027;&#x7684; <code>setState</code> &#x673A;&#x5236;&#x6765;&#x7BA1;&#x7406;&#x3002;
&#x54CD;&#x5E94;&#x5F0F;&#x72B6;&#x6001;&#x4F1A;&#x88AB; <code>render</code> &#x63D0;&#x53D6;&#x8C03;&#x7528;&#xFF0C;&#x4F46;&#x4E0D;&#x4F1A;&#x8C03;&#x7528;&#x5176;&#x5B83; React &#x7684;&#x751F;&#x547D;&#x5468;&#x671F;&#x65B9;&#x6CD5;&#xFF0C;&#x9664;&#x4E86; <code>componentWillUpdate</code> &#x548C; <code>componentDidUpdate</code> &#x3002;
&#x5982;&#x679C;&#x4F60;&#x9700;&#x8981;&#x7528;&#x5230;&#x5176;&#x4ED6; React &#x751F;&#x547D;&#x5468;&#x671F;&#x65B9;&#x6CD5; &#xFF0C;&#x53EA;&#x9700;&#x4F7F;&#x7528;&#x57FA;&#x4E8E; <code>state</code> &#x7684;&#x5E38;&#x89C4; React API &#x5373;&#x53EF;&#x3002;</p>
<p>&#x4E0A;&#x9762;&#x7684;&#x4F8B;&#x5B50;&#x8FD8;&#x53EF;&#x4EE5;&#x8FD9;&#x6837;&#x5199;:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> {observer} <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;mobx-react&quot;</span>
<span class="hljs-keyword">import</span> {observable} <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;mobx&quot;</span>

@observer <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Timer</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
    @observable secondsPassed = <span class="hljs-number">0</span>

    componentWillMount() {
        setInterval(() =&gt; {
            <span class="hljs-keyword">this</span>.secondsPassed++
        }, <span class="hljs-number">1000</span>)
    }

    render() {
        <span class="hljs-keyword">return</span> (<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>Seconds passed: { this.secondsPassed } <span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span></span> )
    }
}

ReactDOM.render(<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Timer</span> /&gt;</span>, document.body)
</span></code></pre>
<p>&#x5BF9;&#x4E8E;&#x4F7F;&#x7528;&#x53EF;&#x89C2;&#x5BDF;&#x7684;&#x5C40;&#x90E8;&#x7EC4;&#x4EF6;&#x72B6;&#x6001;&#x66F4;&#x591A;&#x7684;&#x4F18;&#x52BF;&#xFF0C;&#x8BF7;&#x53C2;&#x89C1;<a href="https://medium.com/@mweststrate/3-reasons-why-i-stopped-using-react-setstate-ab73fc67a42e" target="_blank">&#x4E3A;&#x4EC0;&#x4E48;&#x6211;&#x4E0D;&#x518D;&#x4F7F;&#x7528; <code>setState</code> &#x7684;&#x4E09;&#x4E2A;&#x7406;&#x7531;</a>&#x3002;</p>
<h2 id="&#x4F7F;&#x7528;-inject-&#x5C06;&#x7EC4;&#x4EF6;&#x8FDE;&#x63A5;&#x5230;&#x63D0;&#x4F9B;&#x7684;-stores"><a name="&#x4F7F;&#x7528;-inject-&#x5C06;&#x7EC4;&#x4EF6;&#x8FDE;&#x63A5;&#x5230;&#x63D0;&#x4F9B;&#x7684;-stores" class="plugin-anchor" href="#&#x4F7F;&#x7528;-inject-&#x5C06;&#x7EC4;&#x4EF6;&#x8FDE;&#x63A5;&#x5230;&#x63D0;&#x4F9B;&#x7684;-stores"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4F7F;&#x7528; <code>inject</code> &#x5C06;&#x7EC4;&#x4EF6;&#x8FDE;&#x63A5;&#x5230;&#x63D0;&#x4F9B;&#x7684; stores</h2>
<p><a style="color: white; background:green;padding:5px;margin:5px;border-radius:2px" href="https://egghead.io/lessons/react-connect-mobx-observer-components-to-the-store-with-the-react-provider" target="_blank">egghead.io &#x7B2C;8&#x8BFE;: &#x4F7F;&#x7528; Provider &#x6CE8;&#x5165; stores</a></p>
<p><code>mobx-react</code> &#x5305;&#x8FD8;&#x63D0;&#x4F9B;&#x4E86; <code>Provider</code> &#x7EC4;&#x4EF6;&#xFF0C;&#x5B83;&#x4F7F;&#x7528;&#x4E86; React &#x7684;&#x4E0A;&#x4E0B;&#x6587;(context)&#x673A;&#x5236;&#xFF0C;&#x53EF;&#x4EE5;&#x7528;&#x6765;&#x5411;&#x4E0B;&#x4F20;&#x9012; <code>stores</code>&#x3002;
&#x8981;&#x8FDE;&#x63A5;&#x5230;&#x8FD9;&#x4E9B; stores&#xFF0C;&#x9700;&#x8981;&#x4F20;&#x9012;&#x4E00;&#x4E2A; stores &#x540D;&#x79F0;&#x7684;&#x5217;&#x8868;&#x7ED9; <code>inject</code>&#xFF0C;&#x8FD9;&#x4F7F;&#x5F97; stores &#x53EF;&#x4EE5;&#x4F5C;&#x4E3A;&#x7EC4;&#x4EF6;&#x7684; <code>props</code> &#x4F7F;&#x7528;&#x3002;</p>
<p><em> &#x6CE8;&#x610F;: &#x4ECE; mobx-react 4&#x5F00;&#x59CB;&#xFF0C;&#x6CE8;&#x5165; stores &#x7684;&#x8BED;&#x6CD5;&#x53D1;&#x751F;&#x4E86;&#x53D8;&#x5316;&#xFF0C;&#x5E94;&#x8BE5;&#x4E00;&#x76F4;&#x4F7F;&#x7528;  <code>inject(stores)(component)</code> &#x6216; <code>@inject(stores) class Component...</code>&#x3002;
&#x76F4;&#x63A5;&#x4F20;&#x9012; store &#x540D;&#x79F0;&#x7ED9; <code>observer</code> &#x7684;&#x65B9;&#x5F0F;&#x5DF2;&#x5E9F;&#x5F03;&#x3002; </em></p>
<p>&#x793A;&#x4F8B;:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> colors = observable({
   foreground: <span class="hljs-string">&apos;#000&apos;</span>,
   background: <span class="hljs-string">&apos;#fff&apos;</span>
});

<span class="hljs-keyword">const</span> App = () =&gt;
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Provider</span> <span class="hljs-attr">colors</span>=<span class="hljs-string">{colors}</span>&gt;</span>
     <span class="hljs-tag">&lt;<span class="hljs-name">app</span> <span class="hljs-attr">stuff...</span> /&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">Provider</span>&gt;</span>;

const Button = inject(&quot;colors&quot;)(observer(({ colors, label, onClick }) =&gt;
  <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{{</span>
      <span class="hljs-attr">color:</span> <span class="hljs-attr">colors.foreground</span>,
      <span class="hljs-attr">backgroundColor:</span> <span class="hljs-attr">colors.background</span>
    }}
    <span class="hljs-attr">onClick</span>=<span class="hljs-string">{onClick}</span>
  &gt;</span>{label}<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
));

// &#x7A0D;&#x540E;..
colors.foreground = &apos;blue&apos;;
// &#x6240;&#x6709;button&#x90FD;&#x4F1A;&#x66F4;&#x65B0;
</span></code></pre>
<p>&#x66F4;&#x591A;&#x8D44;&#x6599;&#xFF0C;&#x8BF7;&#x53C2;&#x89C1; <a href="https://github.com/mobxjs/mobx-react#provider-and-inject" target="_blank"><code>mobx-react</code> &#x6587;&#x6863;</a>&#x3002;</p>
<h2 id="&#x4F55;&#x65F6;&#x4F7F;&#x7528;-observer"><a name="&#x4F55;&#x65F6;&#x4F7F;&#x7528;-observer" class="plugin-anchor" href="#&#x4F55;&#x65F6;&#x4F7F;&#x7528;-observer"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4F55;&#x65F6;&#x4F7F;&#x7528; <code>observer</code>?</h2>
<p>&#x7B80;&#x5355;&#x6765;&#x8BF4;: <em>&#x6240;&#x6709;&#x6E32;&#x67D3; observable &#x6570;&#x636E;&#x7684;&#x7EC4;&#x4EF6;</em>&#x3002;
&#x5982;&#x679C;&#x4F60;&#x4E0D;&#x60F3;&#x5C06;&#x7EC4;&#x4EF6;&#x6807;&#x8BB0;&#x4E3A; observer&#xFF0C;&#x4F8B;&#x5982;&#x4E3A;&#x4E86;&#x51CF;&#x5C11;&#x901A;&#x7528;&#x7EC4;&#x4EF6;&#x5305;&#x7684;&#x4F9D;&#x8D56;&#xFF0C;&#x8BF7;&#x786E;&#x4FDD;&#x53EA;&#x4F20;&#x9012;&#x666E;&#x901A;&#x6570;&#x636E;&#x3002;</p>
<p>&#x4F7F;&#x7528; <code>@observer</code> &#x7684;&#x8BDD;&#xFF0C;&#x4E0D;&#x518D;&#x9700;&#x8981;&#x4ECE;&#x6E32;&#x67D3;&#x76EE;&#x7684;&#x4E0A;&#x6765;&#x533A;&#x5206;&#x662F;&#x201C;&#x667A;&#x80FD;&#x7EC4;&#x4EF6;&#x201D;&#x8FD8;&#x662F;&#x201C;&#x65E0;&#x8111;&#x201D;&#x7EC4;&#x4EF6;&#x3002;
&#x5728;&#x7EC4;&#x4EF6;&#x7684;&#x4E8B;&#x4EF6;&#x5904;&#x7406;&#x3001;&#x53D1;&#x8D77;&#x8BF7;&#x6C42;&#x7B49;&#x65B9;&#x9762;&#xFF0C;&#x5B83;&#x4E5F;&#x662F;&#x4E00;&#x4E2A;&#x5F88;&#x597D;&#x7684;&#x5206;&#x79BB;&#x5173;&#x6CE8;&#x70B9;&#x3002;
&#x5F53;&#x6240;&#x6709;&#x7EC4;&#x4EF6;&#x5B83;&#x4EEC;<strong>&#x81EA;&#x5DF1;&#x7684;</strong>&#x4F9D;&#x8D56;&#x9879;&#x6709;&#x53D8;&#x5316;&#x65F6;&#xFF0C;&#x7EC4;&#x4EF6;&#x81EA;&#x5DF1;&#x4F1A;&#x54CD;&#x5E94;&#x66F4;&#x65B0;&#x3002;
&#x800C;&#x5B83;&#x7684;&#x8BA1;&#x7B97;&#x5F00;&#x9500;&#x662F;&#x53EF;&#x4EE5;&#x5FFD;&#x7565;&#x7684;&#xFF0C;&#x5E76;&#x4E14;&#x5B83;&#x4F1A;&#x786E;&#x4FDD;&#x4E0D;&#x7BA1;&#x4F55;&#x65F6;&#xFF0C;&#x53EA;&#x8981;&#x5F53;&#x4F60;&#x5F00;&#x59CB;&#x4F7F;&#x7528; observable &#x6570;&#x636E;&#x65F6;&#xFF0C;&#x7EC4;&#x4EF6;&#x90FD;&#x5C06;&#x4F1A;&#x54CD;&#x5E94;&#x5B83;&#x7684;&#x53D8;&#x5316;&#x3002;
&#x66F4;&#x591A;&#x8BE6;&#x60C5;&#xFF0C;&#x8BF7;&#x53C2;&#x89C1; <a href="https://www.reddit.com/r/reactjs/comments/4vnxg5/free_eggheadio_course_learn_mobx_react_in_30/d61oh0l" target="_blank">&#x8FD9;&#x91CC;</a>&#x3002;</p>
<h2 id="observer-&#x548C;-purecomponent"><a name="observer-&#x548C;-purecomponent" class="plugin-anchor" href="#observer-&#x548C;-purecomponent"><i class="fa fa-link" aria-hidden="true"></i></a><code>observer</code> &#x548C; <code>PureComponent</code></h2>
<p>&#x5982;&#x679C;&#x4F20;&#x9012;&#x7ED9;&#x7EC4;&#x4EF6;&#x7684;&#x6570;&#x636E;&#x662F;&#x54CD;&#x5E94;&#x5F0F;&#x7684;,<code>observer</code> &#x8FD8;&#x53EF;&#x4EE5;&#x9632;&#x6B62;&#x5F53;&#x7EC4;&#x4EF6;&#x7684; <em>props</em> &#x53EA;&#x662F;&#x6D45;&#x6539;&#x53D8;&#x65F6;&#x7684;&#x91CD;&#x65B0;&#x6E32;&#x67D3;&#xFF0C;&#x8FD9;&#x662F;&#x5F88;&#x6709;&#x610F;&#x4E49;&#x7684;&#x3002;
&#x8FD9;&#x4E2A;&#x884C;&#x4E3A;&#x4E0E; <a href="https://reactjs.org/docs/react-api.html#reactpurecomponent" target="_blank">React PureComponent</a> &#x76F8;&#x4F3C;&#xFF0C;&#x4E0D;&#x540C;&#x5728;&#x4E8E;&#x8FD9;&#x91CC;&#x7684; <em>state</em> &#x7684;&#x66F4;&#x6539;&#x4ECD;&#x7136;&#x4F1A;&#x88AB;&#x5904;&#x7406;&#x3002;
&#x5982;&#x679C;&#x4E00;&#x4E2A;&#x7EC4;&#x4EF6;&#x63D0;&#x4F9B;&#x4E86;&#x5B83;&#x81EA;&#x5DF1;&#x7684; <code>shouldComponentUpdate</code>&#xFF0C;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x4F1A;&#x88AB;&#x4F18;&#x5148;&#x8C03;&#x7528;&#x3002;
&#x60F3;&#x8981;&#x66F4;&#x8BE6;&#x7EC6;&#x7684;&#x89E3;&#x91CA;&#xFF0C;&#x8BF7;&#x53C2;&#x89C1;&#x8FD9;&#x4E2A; <a href="https://github.com/mobxjs/mobx/issues/101" target="_blank">github issue</a>&#x3002;</p>
<h2 id="componentwillreact-&#x751F;&#x547D;&#x5468;&#x671F;&#x94A9;&#x5B50;"><a name="componentwillreact-&#x751F;&#x547D;&#x5468;&#x671F;&#x94A9;&#x5B50;" class="plugin-anchor" href="#componentwillreact-&#x751F;&#x547D;&#x5468;&#x671F;&#x94A9;&#x5B50;"><i class="fa fa-link" aria-hidden="true"></i></a><code>componentWillReact</code> (&#x751F;&#x547D;&#x5468;&#x671F;&#x94A9;&#x5B50;)</h2>
<p>React &#x7EC4;&#x4EF6;&#x901A;&#x5E38;&#x5728;&#x65B0;&#x7684;&#x5806;&#x6808;&#x4E0A;&#x6E32;&#x67D3;&#xFF0C;&#x8FD9;&#x4F7F;&#x5F97;&#x901A;&#x5E38;&#x5F88;&#x96BE;&#x5F04;&#x6E05;&#x695A;&#x662F;&#x4EC0;&#x4E48;<strong>&#x5BFC;&#x81F4;</strong>&#x7EC4;&#x4EF6;&#x7684;&#x91CD;&#x65B0;&#x6E32;&#x67D3;&#x3002;
&#x5F53;&#x4F7F;&#x7528; <code>mobx-react</code> &#x65F6;&#x53EF;&#x4EE5;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x751F;&#x547D;&#x5468;&#x671F;&#x94A9;&#x5B50;&#x51FD;&#x6570; <code>componentWillReact</code>(&#x4E00;&#x8BED;&#x53CC;&#x5173;)&#x3002;&#x5F53;&#x7EC4;&#x4EF6;&#x56E0;&#x4E3A;&#x5B83;&#x89C2;&#x5BDF;&#x7684;&#x6570;&#x636E;&#x53D1;&#x751F;&#x4E86;&#x6539;&#x53D8;&#xFF0C;&#x5B83;&#x4F1A;&#x5B89;&#x6392;&#x91CD;&#x65B0;&#x6E32;&#x67D3;&#xFF0C;&#x8FD9;&#x4E2A;&#x65F6;&#x5019; <code>componentWillReact</code> &#x4F1A;&#x88AB;&#x89E6;&#x53D1;&#x3002;&#x8FD9;&#x4F7F;&#x5F97;&#x5B83;&#x5F88;&#x5BB9;&#x6613;&#x8FFD;&#x6EAF;&#x6E32;&#x67D3;&#x5E76;&#x627E;&#x5230;&#x5BFC;&#x81F4;&#x6E32;&#x67D3;&#x7684;&#x64CD;&#x4F5C;(action)&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> {observer} <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;mobx-react&quot;</span>;

@observer <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">TodoView</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
    componentWillReact() {
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;I will re-render, since the todo has changed!&quot;</span>);
    }

    render() {
        <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>this.props.todo.title<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>;
    }
}
</code></pre>
<ul>
<li><code>componentWillReact</code> &#x4E0D;&#x63A5;&#x6536;&#x53C2;&#x6570;</li>
<li><code>componentWillReact</code> &#x521D;&#x59CB;&#x5316;&#x6E32;&#x67D3;&#x524D;&#x4E0D;&#x4F1A;&#x89E6;&#x53D1; (&#x4F7F;&#x7528; <code>componentWillMount</code> &#x66FF;&#x4EE3;)</li>
<li><code>componentWillReact</code> &#x5BF9;&#x4E8E; mobx-react@4+, &#x5F53;&#x63A5;&#x6536;&#x65B0;&#x7684; props &#x65F6;&#x5E76;&#x5728; <code>setState</code> &#x8C03;&#x7528;&#x540E;&#x4F1A;&#x89E6;&#x53D1;&#x6B64;&#x94A9;&#x5B50;</li>
</ul>
<h2 id="&#x4F18;&#x5316;&#x7EC4;&#x4EF6;"><a name="&#x4F18;&#x5316;&#x7EC4;&#x4EF6;" class="plugin-anchor" href="#&#x4F18;&#x5316;&#x7EC4;&#x4EF6;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4F18;&#x5316;&#x7EC4;&#x4EF6;</h2>
<p>&#x8BF7;&#x53C2;&#x89C1;&#x76F8;&#x5173;<a href="../best/react-performance.html">&#x7AE0;&#x8282;</a>&#x3002;</p>
<h2 id="mobx-react-devtools"><a name="mobx-react-devtools" class="plugin-anchor" href="#mobx-react-devtools"><i class="fa fa-link" aria-hidden="true"></i></a>MobX-React-DevTools</h2>
<p>&#x7ED3;&#x5408; <code>@observer</code>&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; MobX-React-DevTools &#xFF0C;&#x5B83;&#x7CBE;&#x786E;&#x5730;&#x663E;&#x793A;&#x4E86;&#x4F55;&#x65F6;&#x91CD;&#x65B0;&#x6E32;&#x67D3;&#x7EC4;&#x4EF6;&#xFF0C;&#x5E76;&#x4E14;&#x53EF;&#x4EE5;&#x68C0;&#x67E5;&#x7EC4;&#x4EF6;&#x7684;&#x6570;&#x636E;&#x4F9D;&#x8D56;&#x5173;&#x7CFB;&#x3002;
&#x8BE6;&#x60C5;&#x8BF7;&#x53C2;&#x89C1; <a href="../best/devtools.html">&#x5F00;&#x53D1;&#x8005;&#x5DE5;&#x5177;</a> &#x3002;</p>
<h2 id="observer-&#x7EC4;&#x4EF6;&#x7279;&#x6027;"><a name="observer-&#x7EC4;&#x4EF6;&#x7279;&#x6027;" class="plugin-anchor" href="#observer-&#x7EC4;&#x4EF6;&#x7279;&#x6027;"><i class="fa fa-link" aria-hidden="true"></i></a>observer &#x7EC4;&#x4EF6;&#x7279;&#x6027;</h2>
<ul>
<li>Observer &#x4EC5;&#x8BA2;&#x9605;&#x5728;&#x4E0A;&#x6B21;&#x6E32;&#x67D3;&#x671F;&#x95F4;&#x6D3B;&#x8DC3;&#x4F7F;&#x7528;&#x7684;&#x6570;&#x636E;&#x7ED3;&#x6784;&#x3002;&#x8FD9;&#x610F;&#x5473;&#x7740;&#x4F60;&#x4E0D;&#x4F1A;&#x8BA2;&#x9605;&#x4E0D;&#x8DB3;(under-subscribe)&#x6216;&#x8005;&#x8FC7;&#x5EA6;&#x8BA2;&#x9605;(over-subscribe)&#x3002;&#x4F60;&#x751A;&#x81F3;&#x53EF;&#x4EE5;&#x5728;&#x6E32;&#x67D3;&#x65B9;&#x6CD5;&#x4E2D;&#x4F7F;&#x7528;&#x4EC5;&#x5728;&#x672A;&#x6765;&#x65F6;&#x95F4;&#x6BB5;&#x53EF;&#x7528;&#x7684;&#x6570;&#x636E;&#x3002; &#x8FD9;&#x662F;&#x5F02;&#x6B65;&#x52A0;&#x8F7D;&#x6570;&#x636E;&#x7684;&#x7406;&#x60F3;&#x9009;&#x62E9;&#x3002;</li>
<li>&#x4F60;&#x4E0D;&#x9700;&#x8981;&#x58F0;&#x660E;&#x7EC4;&#x4EF6;&#x5C06;&#x4F7F;&#x7528;&#x4EC0;&#x4E48;&#x6570;&#x636E;&#x3002;&#x76F8;&#x53CD;&#xFF0C;&#x4F9D;&#x8D56;&#x5173;&#x7CFB;&#x5728;&#x8FD0;&#x884C;&#x65F6;&#x4F1A;&#x786E;&#x5B9A;&#x5E76;&#x4EE5;&#x975E;&#x5E38;&#x7EC6;&#x7C92;&#x5EA6;&#x7684;&#x65B9;&#x5F0F;&#x8FDB;&#x884C;&#x8DDF;&#x8E2A;&#x3002;</li>
<li>&#x901A;&#x5E38;&#xFF0C;&#x54CD;&#x5E94;&#x5F0F;&#x7EC4;&#x4EF6;&#x6CA1;&#x6709;&#x6216;&#x5F88;&#x5C11;&#x6709;&#x72B6;&#x6001;&#xFF0C;&#x56E0;&#x4E3A;&#x5728;&#x4E0E;&#x5176;&#x4ED6;&#x7EC4;&#x4EF6;&#x5171;&#x4EAB;&#x7684;&#x5BF9;&#x8C61;&#x4E2D;&#x5C01;&#x88C5;(&#x89C6;&#x56FE;)&#x72B6;&#x6001;&#x901A;&#x5E38;&#x66F4;&#x65B9;&#x4FBF;&#x3002;&#x4F46;&#x4F60;&#x4ECD;&#x7136;&#x53EF;&#x4EE5;&#x81EA;&#x7531;&#x5730;&#x4F7F;&#x7528;&#x72B6;&#x6001;&#x3002;</li>
<li><code>@observer</code> &#x4EE5;&#x548C; <code>PureComponent</code> &#x540C;&#x6837;&#x7684;&#x65B9;&#x5F0F;&#x5B9E;&#x73B0;&#x4E86; <code>shouldComponentUpdate</code>&#xFF0C;&#x56E0;&#x6B64;&#x5B50;&#x7EC4;&#x4EF6;&#x53EF;&#x4EE5;&#x907F;&#x514D;&#x4E0D;&#x5FC5;&#x8981;&#x7684;&#x91CD;&#x65B0;&#x6E32;&#x67D3;&#x3002;</li>
<li>&#x54CD;&#x5E94;&#x5F0F;&#x7EC4;&#x4EF6;&#x5355;&#x65B9;&#x9762;&#x52A0;&#x8F7D;&#x6570;&#x636E;&#xFF0C;&#x5373;&#x4F7F;&#x5B50;&#x7EC4;&#x4EF6;&#x8981;&#x91CD;&#x65B0;&#x6E32;&#x67D3;&#xFF0C;&#x7236;&#x7EC4;&#x4EF6;&#x4E5F;&#x4E0D;&#x4F1A;&#x8FDB;&#x884C;&#x4E0D;&#x5FC5;&#x8981;&#x5730;&#x91CD;&#x65B0;&#x6E32;&#x67D3;&#x3002;</li>
<li><code>@observer</code> &#x4E0D;&#x4F9D;&#x8D56;&#x4E8E; React &#x7684;&#x4E0A;&#x4E0B;&#x6587;&#x7CFB;&#x7EDF;&#x3002;</li>
<li>mobx-react@4+ &#x4E2D;&#xFF0C;observer &#x7EC4;&#x4EF6;&#x7684;props &#x5BF9;&#x8C61;&#x548C; state &#x5BF9;&#x8C61;&#x90FD;&#x4F1A;&#x81EA;&#x52A8;&#x5730;&#x8F6C;&#x53D8;&#x4E3A; observable&#xFF0C;&#x8FD9;&#x4F7F;&#x5F97;&#x521B;&#x5EFA; @computed &#x5C5E;&#x6027;&#x66F4;&#x5BB9;&#x6613;&#xFF0C;@computed &#x5C5E;&#x6027;&#x662F;&#x6839;&#x636E;&#x7EC4;&#x4EF6;&#x5185;&#x90E8;&#x7684; props &#x63A8;&#x5BFC;&#x5F97;&#x5230;&#x7684;&#x3002;&#x5982;&#x679C;&#x5728; <code>@observer</code> &#x7EC4;&#x4EF6;&#x4E2D;&#x5305;&#x542B; reaction(&#x4F8B;&#x5982; <code>autorun</code>) &#x7684;&#x8BDD;&#xFF0C;&#x5F53; reaction &#x4F7F;&#x7528;&#x7684;&#x7279;&#x5B9A;&#x5C5E;&#x6027;&#x4E0D;&#x518D;&#x6539;&#x53D8;&#x65F6;&#xFF0C;reaction &#x662F;&#x4E0D;&#x4F1A;&#x518D;&#x91CD;&#x65B0;&#x8FD0;&#x884C;&#x7684;&#xFF0C;&#x5728; reaction &#x4E2D;&#x4F7F;&#x7528;&#x7684;&#x7279;&#x5B9A; props &#x4E00;&#x5B9A;&#x8981;&#x95F4;&#x63A5;&#x5F15;&#x7528;(&#x4F8B;&#x5982; <code>const myProp = props.myProp</code>)&#x3002;&#x4E0D;&#x7136;&#xFF0C;&#x5982;&#x679C;&#x4F60;&#x5728; reaction &#x4E2D;&#x5F15;&#x7528;&#x4E86; <code>props.myProp</code>&#xFF0C;&#x90A3;&#x4E48; props &#x7684;<strong>&#x4EFB;&#x4F55;</strong>&#x6539;&#x53D8;&#x90FD;&#x4F1A;&#x5BFC;&#x81F4; reaction &#x7684;&#x91CD;&#x65B0;&#x8FD0;&#x884C;&#x3002;&#x5BF9;&#x4E8E; React-Router &#x7684;&#x5178;&#x578B;&#x7528;&#x4F8B;&#xFF0C;&#x8BF7;&#x53C2;&#x89C1;<a href="https://alexhisen.gitbooks.io/mobx-recipes/content/observable-based-routing.html" target="_blank">&#x8FD9;&#x7BC7;&#x6587;&#x7AE0;</a>&#x3002;</li>
</ul>
<h2 id="&#x5728;&#x7F16;&#x8BD1;&#x5668;&#x4E2D;&#x542F;&#x7528;&#x88C5;&#x9970;&#x5668;"><a name="&#x5728;&#x7F16;&#x8BD1;&#x5668;&#x4E2D;&#x542F;&#x7528;&#x88C5;&#x9970;&#x5668;" class="plugin-anchor" href="#&#x5728;&#x7F16;&#x8BD1;&#x5668;&#x4E2D;&#x542F;&#x7528;&#x88C5;&#x9970;&#x5668;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x5728;&#x7F16;&#x8BD1;&#x5668;&#x4E2D;&#x542F;&#x7528;&#x88C5;&#x9970;&#x5668;</h2>
<p>&#x5728;&#x4F7F;&#x7528; TypeScript &#x6216; Babel &#x8FD9;&#x4E9B;&#x7B49;&#x5F85;ES&#x6807;&#x51C6;&#x5B9A;&#x4E49;&#x7684;&#x7F16;&#x8BD1;&#x5668;&#x65F6;&#xFF0C;&#x9ED8;&#x8BA4;&#x60C5;&#x51B5;&#x4E0B;&#x662F;&#x4E0D;&#x652F;&#x6301;&#x88C5;&#x9970;&#x5668;&#x7684;&#x3002;</p>
<ul>
<li>&#x5BF9;&#x4E8E; <em>typescript</em>&#xFF0C;&#x542F;&#x7528; <code>--experimentalDecorators</code> &#x7F16;&#x8BD1;&#x5668;&#x6807;&#x8BC6;&#x6216;&#x8005;&#x5728; <code>tsconfig.json</code> &#x4E2D;&#x628A;&#x7F16;&#x8BD1;&#x5668;&#x5C5E;&#x6027; <code>experimentalDecorators</code> &#x8BBE;&#x7F6E;&#x4E3A; <code>true</code> (&#x63A8;&#x8350;&#x505A;&#x6CD5;)</li>
<li>&#x5BF9;&#x4E8E; <em>babel5</em>&#xFF0C;&#x786E;&#x4FDD;&#x628A; <code>--stage 0</code> &#x4F20;&#x9012;&#x7ED9; Babel CLI</li>
<li>&#x5BF9;&#x4E8E; <em>babel6</em>&#xFF0C;&#x53C2;&#x89C1;&#x6B64; <a href="https://github.com/mobxjs/mobx/issues/105" target="_blank">issue</a> &#x4E2D;&#x5EFA;&#x8BAE;&#x7684;&#x793A;&#x4F8B;&#x914D;&#x7F6E;&#x3002;</li>
</ul>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="reaction.html" class="navigation navigation-prev " aria-label="Previous page: 4.4. reaction">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../best/react.html" class="navigation navigation-next " aria-label="Next page: 4.6. 理解 MobX 对何作出响应">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"4.5. (@)observer","level":"1.4.5","depth":2,"next":{"title":"4.6. 理解 MobX 对何作出响应","level":"1.4.6","depth":2,"path":"best/react.md","ref":"best/react.md","articles":[]},"previous":{"title":"4.4. reaction","level":"1.4.4","depth":2,"path":"refguide/reaction.md","ref":"refguide/reaction.md","articles":[]},"dir":"ltr"},"config":{"plugins":["github","edit-link","anchors","copy-code-button","github-buttons"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"github":{"url":"https://github.com/SangKa/mobx-docs-cn"},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"github-buttons":{"buttons":[{"user":"SangKa","repo":"MobX-Docs-CN","type":"star","size":"small","count":true},{"user":"SangKa","repo":"MobX-Docs-CN","type":"fork","size":"small","count":true}]},"copy-code-button":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"edit-link":{"label":"完善文档","base":"https://github.com/SangKa/mobx-docs-cn/tree/master/docs"},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"anchors":{}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"MobX 中文文档","gitbook":"3.2.2"},"file":{"path":"refguide/observer-component.md","mtime":"2018-09-18T13:06:39.964Z","type":"markdown"},"gitbook":{"version":"3.2.2","time":"2018-09-18T13:06:52.064Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-github/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-edit-link/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-copy-code-button/toggle.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-github-buttons/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

